<?php

use Fisharebest\Webtrees\Http\RequestHandlers\MapDataDelete;
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataList;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Webtrees;
use Illuminate\Support\Collection;

/**
 * @var array<stdClass>               $active
 * @var Collection<Tree>              $all_trees
 * @var array<string,string>          $breadcrumbs
 * @var int                           $parent_id
 * @var array<mixed>                  $placelist
 * @var PlaceHierarchyListModule|null $list_module
 * @var string                        $title
 */

?>

<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>

<h1><?= $title ?></h1>

<p>
    <label>
        <input id="hide-unused-locations" type="checkbox" data-toggle="collapse" data-target=".unused-location">
        <?= I18N::translate('Hide unused locations') ?>
    </label>
</p>

<table class="table table-bordered table-striped table-sm table-hover wt-table-locations">
    <thead class="thead-dark">
        <tr>
            <th><?= I18N::translate('Place') ?></th>
            <th><?= I18N::translate('Latitude') ?></th>
            <th><?= I18N::translate('Longitude') ?></th>
            <!--
            <th><?= I18N::translate('Flag') ?> </th>
            -->
            <th><?= I18N::translate('Edit') ?></th>
            <th><?= I18N::translate('Facts and events') ?></th>
            <th><?= I18N::translate('Delete') ?></th>
        </tr>
    </thead>

    <tbody>
    <?php foreach ($placelist as $place) : ?>
        <tr class="<?= $active[$place->pl_place] ?? null ? '' : 'unused-location collapse show' ?>">
            <th scope="row">
                <a href="<?= e(route(MapDataList::class, ['parent_id' => $place->pl_id])) ?>">
                    <?= e($place->pl_place) ?>
                    <?php if ($place->no_coord > 0): ?>
                        <span class="badge badge-pill badge-secondary">
                            <?= view('icons/warning') ?>
                            <?= I18N::number($place->no_coord) ?>
                            /
                            <?= I18N::number($place->child_count) ?>
                        </span>
                    <?php elseif ($place->child_count > 0): ?>
                        <span class="badge badge-pill badge-secondary">
                            <?= I18N::number($place->child_count) ?>
                        </span>
                    <?php endif ?>
                </a>
            </th>

            <td dir="ltr">
                <?php if ((string) $place->pl_lati === '') : ?>
                    <?= view('icons/warning') ?>
                <?php else : ?>
                    <?= strtr($place->pl_lati, ['N' => '', 'S' => '-', ',' => '.']) ?>
                <?php endif ?>
            </td>

            <td dir="ltr">
                <?php if ((string) $place->pl_long === '') : ?>
                    <?= view('icons/warning') ?>
                <?php else : ?>
                    <?= strtr($place->pl_long, ['E' => '', 'W' => '-', ',' => '.']) ?>
                <?php endif ?>
            </td>

            <!--
            <td>
                <?php if (is_file(Webtrees::MODULES_PATH . 'openstreetmap/' . $place->pl_icon)) : ?>
                    <img src="<?= e(Webtrees::MODULES_PATH . 'openstreetmap/' . $place->pl_icon) ?>" width="25" height="15" alt="<?= I18N::translate('Flag of %s', $place->pl_place) ?>">
                <?php endif ?>
            </td>
            -->

            <td>
                <a class="btn btn-primary" href="<?= e(route('map-data-edit', ['place_id'  => $place->pl_id, 'parent_id' => $place->pl_parent_id])) ?>" title="<?= I18N::translate('Edit') ?>">
                    <?= view('icons/edit') ?>
                    <span class="sr-only">
                        <?= I18N::translate('Edit') ?>
                    </span>
                </a>
            </td>

            <td>
                <?php if (($list_module !== null) && array_key_exists($place->pl_place, $active)): ?>
                    <?php if (count($active[$place->pl_place]) === 1): ?>
                        <a class="btn btn-link" href="<?= e($list_module->listUrl($all_trees->get($active[$place->pl_place][0]->tree_name), ['place_id' => $active[$place->pl_place][0]->p_id, 'action2' => 'hierarchy-e'])) ?>">
                            <?= e($active[$place->pl_place][0]->tree_title) ?>
                        </a>
                    <?php else: ?>
                        <div class="dropdown">
                            <a class="btn btn-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <?= I18N::plural('%s family tree', '%s family trees', count($active[$place->pl_place]), I18N::number(count($active[$place->pl_place]))) ?>
                            </a>

                            <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                                <?php foreach ($active[$place->pl_place] as $link): ?>
                                    <a class="dropdown-item" href="<?= e($list_module->listUrl($all_trees->get($active[$place->pl_place][0]->tree_name), ['place_id' => $link->p_id])) ?>">
                                        <?= e($link->tree_title) ?>
                                    </a>
                                <?php endforeach ?>
                            </div>
                        </div>
                    <?php endif ?>
                <?php endif ?>
            </td>

            <td>
                <?php if (!array_key_exists($place->pl_place, $active)): ?>
                    <form method="post" action="<?= e(route(MapDataDelete::class, ['place_id' => $place->pl_id])) ?>">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-danger" aria-label="<?= I18N::translate('delete') ?>" data-confirm="<?= I18N::translate('Remove this location?') ?>">
                            <?= view('icons/delete') ?>
                        </button>
                    </form>
                <?php endif ?>
            </td>
        </tr>
    <?php endforeach ?>
    </tbody>

    <tfoot>
        <tr>
            <td colspan="7">
                <a class="btn btn-primary" href="<?= e(route('map-data-edit', ['place_id' => '0', 'parent_id' => $parent_id])) ?>">
                    <?= view('icons/add') ?>
                    <?= /* I18N: A button label. */
                    I18N::translate('add place') ?>
                </a>
                <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton"
                        data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <?= view('icons/download') ?>
                    <?= /* I18N: A button label. */
                    I18N::translate('export file') ?>
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <a class="dropdown-item" href="<?= e(route('locations-export', ['parent_id' => $parent_id, 'format' => 'csv'])) ?>">
                        csv
                    </a>
                    <a class="dropdown-item" href="<?= e(route('locations-export', ['parent_id' => $parent_id, 'format' => 'geojson'])) ?>">
                        geoJSON
                    </a>
                </div>
                <a class="btn btn-primary" href="<?= e(route('locations-import', ['parent_id' => $parent_id])) ?>">
                    <?= view('icons/upload') ?>
                    <?= /* I18N: A button label. */
                    I18N::translate('import file') ?>
                </a>
            </td>
        </tr>
    </tfoot>
</table>

<?php View::push('javascript') ?>
<script>
  'use strict';

  webtrees.persistentToggle("hide-unused-locations");
</script>
<?php View::endpush() ?>
